package com.plian.system.service.cqzc.existapproval;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plian.Tools.TokenUtil;
import com.plian.system.constant.sys.FormTypeConstant;
import com.plian.system.dto.fr.bonds.ExistBondsDTO;
import com.plian.system.entity.fr.bonds.BondsApply;
import com.plian.system.entity.fr.bonds.BondsPlan;
import com.plian.system.entity.fr.bonds.ExistBonds;
import com.plian.system.mapper.fr.bonds.BondsApplyMapper;
import com.plian.system.mapper.fr.bonds.BondsPlanMapper;
import com.plian.system.mapper.fr.bonds.ExistBondsMapper;
import com.plian.system.service.sys.code.IFormCodeService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 说明：发债申请
 */
@Slf4j
@Service
@AllArgsConstructor
public class ExistBondsService extends ServiceImpl<ExistBondsMapper, ExistBonds> {

    private ExistBondsMapper ExistBondsMapper;
    private BondsApplyMapper bondsApplyMapper;
    private BondsPlanMapper bondsPlanMapper;
    private IFormCodeService formCodeService;

    /**
     * 保存
     *
     * @param dto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ExistBonds saveExistBonds(ExistBondsDTO dto) throws Exception {

        ExistBonds entity = new ExistBonds();
        BeanUtils.copyProperties(dto, entity);
        entity.setCode(formCodeService.getCode(FormTypeConstant.CQZC_EXIST_BONDS));
        entity.setCreateCompanyId(TokenUtil.getTokenOrgId());
        entity.setCreateUser(TokenUtil.getTokenUserId());
        DateTime now = DateUtil.date();
        entity.setCreateTime(now);
        entity.setUpdateUser(TokenUtil.getTokenUserId());
        entity.setUpdateTime(now);
        this.save(entity);
        return entity;

    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ExistBonds editExistBonds(ExistBondsDTO dto) throws Exception {

        ExistBonds entity = new ExistBonds();
        BeanUtils.copyProperties(dto, entity);
        entity.setUpdateUser(TokenUtil.getTokenUserId());
        entity.setUpdateTime(DateUtil.date());
        this.updateById(entity);

        return entity;
    }

    @Transactional(rollbackFor = Exception.class)
    public void delByIds(List<String> ids) throws Exception {
        for (String id : ids) {
            ExistBondsMapper.deleteById(id);
        }
    }

}